﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core;

public partial class PageDonVi : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BinData();
            lblCount.InnerText = "Hiện đang có " + DonVi.All().Count() + " mục trong CSDL";
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        gvData.RowDeleting += gvLoaiCanBo_RowDeleting;
        gvData.RowEditing += gvLoaiCanBo_RowEditing;
        gvData.RowUpdating += gvLoaiCanBo_RowUpdating;
        gvData.RowCancelingEdit += gvLoaiCanBo_RowCancelingEdit;
        gvData.PageIndexChanging += gvLoaiCanBo_PageIndexChanging;
    }

    void gvLoaiCanBo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvData.PageIndex = e.NewPageIndex;
        BinData();
    }
    void BinData()
    {
        gvData.DataSource = DonVi.All();
        gvData.DataBind();
    }
    protected void gvLoaiCanBo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        var id = int.Parse(gvData.DataKeys[e.RowIndex]["ID"].ToString());

        DonVi.Delete(id);
        divAlert.InnerHtml = "<b>Thông báo:</b> Xóa dữ liệu thành công." + id;
        divMsg.Visible = true;
        BinData();
    }
    protected void gvLoaiCanBo_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvData.EditIndex = e.NewEditIndex;
        BinData();
    }
    protected void gvLoaiCanBo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvData.EditIndex = -1;
        BinData();
    }

    protected void gvLoaiCanBo_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
         var id = int.Parse(gvData.DataKeys[e.RowIndex]["ID"].ToString());

        var loaiCb = DonVi.Single(id);
      loaiCb.TenDonVi = ((TextBox)gvData.Rows[e.RowIndex].FindControl("txtTenDonVi")).Text;
        DonVi.Update(loaiCb);

        divAlert.InnerHtml = "<b>Thông báo:</b> Cập nhật dữ liệu thành công." + id;
        divMsg.Visible = true;
        BinData();
    }
   

    protected void btnDeleteAll_Click(object sender, EventArgs e)
    {
        string idCollection = "";
        string strID = string.Empty;

        //Loop through GridView rows to find checked rows 
        for (int i = 0; i < gvData.Rows.Count; i++)
        {
            CheckBox chkDelete = (CheckBox)
               gvData.Rows[i].Cells[0].FindControl("chkSelect");
            if (chkDelete != null)
            {
                if (chkDelete.Checked)
                {
                    strID = gvData.Rows[i].Cells[1].Text;
                    idCollection += strID + ",";
                }
            }
        }
        if (idCollection != "")
        {
            try
            {
                foreach (string id in idCollection.Split(','))
                    DonVi.Delete(int.Parse(id));
            }
            catch
            {

            }
        }

        // rebind the GridView


        divAlert.InnerHtml = "<b>Thông báo:</b> Xóa dữ liệu thành công.";
        divMsg.Visible = true;

        BinData();
    }
    protected void btnSaveNew_ServerClick(object sender, EventArgs e)
    {
        string tenLoaiCB = txtTenDonVi.Text;
        if (tenLoaiCB != "")
        {
            var entity = new DonVi { TenDonVi = tenLoaiCB };
            var id = DonVi.Add(entity);
        }
        divAlert.InnerHtml = "<b>Thông báo:</b> Thêm mới dữ liệu thành công.";
        divMsg.Visible = true;
        BinData();
    }
    protected void btnRefresh_ServerClick(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsolutePath);
    }
}